import Vue from 'vue'
import Router from 'vue-router'
import VueCookies from 'vue-cookies'

Vue.use(Router);

/**要记住是渲染在App.vue的router-view下 */
const router = new Router({
  // mode: 'hash',
  mode: 'history',
  linkActiveClass: '',
  linkExactActiveClass: '',
  routes: [

    /**
     * 首页及其下子组件
     */
    {
      path: '/',
      name: 'Home',
      component: () => import('@/views2/Home'),
      children: [{// 首页作品
        path: 'homeworkslist',
        name: 'HomeWorksList',
        component: () => import('@/views2/game/HomeWorksList')
      }, {// 大赛作品
        path: 'game/:gameID',
        name: 'Game',
        component: () => import('@/views2/game/Game')
      }, {// 大赛设计师
        path: 'designerList',
        name: 'DesignerList',
        component: () => import('@/views2/game/DsgsList')
      }, {// 优秀案例
        path: 'EXcellentCase',
        name: 'ExcellentCase',
        component: () => import('@/views2/game/ExcellentCase')
      }, {// 获奖作品
        path: 'prizeList',
        name: 'PrizeList',
        component: () => import('@/views2/game/AwardWorks')
      }]
    },


    /**
     * 其他页面路由
     */
    {// 个人中心
      path: '/personal',
      name: 'personal',
      component: () => import('@/views2/personal/Personal'),
      children: [{
        path: 'panel',
        component: () => import('@/views2/personal/panel/Panel'),
        children: [{
          path: 'table',
          component: () => import('@/views2/personal/panel/WorksTable')
        }, {
          path: 'prize',
          component: () => import('@/views2/personal/panel/Prize')
        }, {
          path: 'create/:id',
          component: () => import('@/views2/personal/panel/Create')
        }]
      }], 
      beforeEnter: (to, from, next) => {
        if(!VueCookies.isKey('designer')) {
          next('/');
        } else {
          next();
        }
      }
    },
    {// 注册
      path: '/register',
      name: 'Register',
      components: {
        default: () => import('@/views2/home/Register')
      }
    },
    {// 报名
      path: '/apply',
      name: 'Apply',
      component: () => import('@/views2/home/Apply'),
      beforeEnter: (to, from, next) => {
        // const isJoin = (JSON.parse(VueCookies.get('designer')) || {isJoin: ''}).isJoin;
        // if(VueCookies.isKey('designer') && (isJoin !== 1)) {// 已登录并且无资格
        //   next();
        // } else {
        //   next('/');
        // }
        next();
      }
    },
    {// 作品详细
      path: '/worksDetail/:worksID',
      name: 'WorksDetail',
      component: () => import('@/views2/game/WorksDetail')
    },
    ,
    {// 设计师详细
      path: '/designerDetail/:id',
      name: 'DesignerDetail',
      component: () => import('@/views/game/DesignerDetail')
    }
  ]
})

export default router;